package com.wangsd.common.component;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.wangsd.web.coupon.entity.ServiceBusiness;
import com.wangsd.web.coupon.service.ServiceBusinessService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WxPayServer {

    @Autowired
    ServiceBusinessService serviceBusinessService;

    public WxPayService getWxPayService(Long serviceId) {
        ServiceBusiness business = serviceBusinessService.getById(serviceId);
        return getWxPayServiceExt(business);
    }

    public WxPayService getWxPayServiceExt(ServiceBusiness business) {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull(business.getAppId()));
        payConfig.setMchId(StringUtils.trimToNull(business.getMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(business.getMchKey()));
        payConfig.setKeyPath(StringUtils.trimToNull(business.getKeyPath()));

        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);

        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }
}
